//
//  HQAddStyleViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/11.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQAddStyleViewController.h"
#import "HQBigImageViewController.h"

@interface HQAddStyleViewController ()
{
    __weak IBOutlet NSLayoutConstraint *_bottomConstraint;
    CGRect _currentRect;
}
@end

@implementation HQAddStyleViewController

/**
 查看大图

 @param sender 按钮
 */
- (IBAction)lookBigImage:(id)sender {
    if(_styleImage.image){
        HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
        bigImageVc.imageData = _styleImage.image;
        [self presentViewController:bigImageVc animated:YES completion:nil];
    }else{
        [self tostView:@"暂无图片"];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentRect =  _headerView.frame;
    
    _currentIndex = 0;
    _addStyleFlg = NO;
    _deleteAllFlg = NO;
    
    // 样式列表
    newsView = [HQStyleListView loadTopHotView];
    newsView.frame = _listBgView.bounds;
    newsView.backgroundColor = [UIColor clearColor];
    newsView.delegate = self;
    [_listBgView addSubview:newsView];
    
    _styleID = @"";
    _newID = @"";
    _editTag = NO;
    if([self.pushType isEqualToString:@"EDIT"]){
        self.title = @"编辑样式数据";
        _dataSource = [[NSMutableArray alloc]init];
        [_styleImage setImageWithURL:[NSURL URLWithString:self.dataInfo[@"s_img"]]];
        _styleID = self.dataInfo[@"s_id"];
        _styleName.text = self.dataInfo[@"s_name"];
        _styleIntrduce.text = self.dataInfo[@"s_introduce"];
        [self test];
    }else{
        self.title = @"设置样式数据";
        // 添加时数组默认有一条数据
        _dataSource = [[NSMutableArray alloc]init];
        HQStyleObject *h = [[HQStyleObject alloc]init];
        h.itemType = @"edit";
        [_dataSource addObject:h];
    }
    [self addKeyBoardObserver];
    [self calculateHeight];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _bottomConstraint.constant = keyboardRect.size.height;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    _bottomConstraint.constant = 0;
}


-(void)addGesture{}

/**
 *  自定义返回按钮触发的方法
 */
-(void)backItem{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要退出此界面吗？ "  preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"再等等" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"退出" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

/**
 选择某个区间的时候列表随之变动
 
 @param hottopView 区间差列表
 @param dataInfo 透传的数据
 */
-(void)selectItem:(HQStyleListView *)hottopView withInfo:(NSDictionary *)dataInfo withIndexPath:(NSIndexPath *)indexPath{
    if([self.pushType isEqualToString:@"EDIT"]){
        _editTag = YES;
        [_sUnit setTitle:dataInfo[@"name"] forState:UIControlStateNormal];
        _sUnitCode = dataInfo[@"i_id"];
        [_sInterval setTitle:[NSString stringWithFormat:@"%@",dataInfo[@"name_ext"]] forState:UIControlStateNormal];
        _sIntervalCode = dataInfo[@"i_eid"];
        [_dataSource removeAllObjects];
        NSMutableArray *a = [[NSMutableArray alloc]initWithArray:dataInfo[@"zj"]];
        for(NSDictionary *dic in a){
            HQStyleObject *a = [[HQStyleObject alloc]init];
            a.mID = dic[@"m_id"];
            a.zujianCode = dic[@"m_cid"];
            a.itemType = @"edit";
            a.zujianVaule = dic[@"m_material"];
            a.unit = dic[@"c_unit"];
            a.zujianName = [NSString stringWithFormat:@"%@/%@",dic[@"name"],dic[@"c_unit"]];
            [_dataSource addObject:a];
        }
        [_tableView reloadData];
    }else{
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您即将要进入修改数据页，是否修改？ "  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if(indexPath.row == 0){
                _addStyleFlg = NO;
                _deleteAllFlg = YES;
            }else{
                _addStyleFlg = YES;
                _deleteAllFlg = YES;
            }
            _editTag = YES;
            [_sUnit setTitle:dataInfo[@"name"] forState:UIControlStateNormal];
            _sUnitCode = dataInfo[@"i_id"];
            [_sInterval setTitle:[NSString stringWithFormat:@"%@",dataInfo[@"name_ext"]] forState:UIControlStateNormal];
            _sIntervalCode = dataInfo[@"i_eid"];
            [_dataSource removeAllObjects];
            NSMutableArray *a = [[NSMutableArray alloc]initWithArray:dataInfo[@"zj"]];
            for(NSDictionary *dic in a){
                HQStyleObject *a = [[HQStyleObject alloc]init];
                a.zujianCode = dic[@"m_cid"];
                a.mID = dic[@"m_id"];
                a.itemType = @"edit";
                a.unit = dic[@"c_unit"];
                a.zujianVaule = dic[@"m_material"];
                a.zujianName = [NSString stringWithFormat:@"%@/%@",dic[@"name"],dic[@"c_unit"]];
                [_dataSource addObject:a];
            }
            [_tableView reloadData];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_addStyleFlg){
        return _dataSource.count;
    }else{
        return _dataSource.count+1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == _dataSource.count){
        // 添加一行样式
        HQAddStyleItemCell *cell = [HQAddStyleItemCell cellWithTableView:tableView];
        return cell;
    }else{
        HQStyleObject *h = _dataSource[indexPath.row];
        if([h.itemType isEqualToString:@"edit"]){
            // 可编辑不可删除的样式
            HQAddStyleSearchCell *cell = [HQAddStyleSearchCell cellWithTableView:tableView];
            cell.dataInfo = _dataSource[indexPath.row];
            cell.tag = 1000+indexPath.row;
//            [cell.itemInput addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.selctStyleItem addTarget:self action:@selector(addStyle:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }else{
            // 可编辑可删除的样式
            HQAddStyleCell *cell = [HQAddStyleCell cellWithTableView:tableView];
            cell.dataInfo = _dataSource[indexPath.row];
            cell.tag = 1000+indexPath.row;
//            [cell.itemInput addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.selctStyleItem addTarget:self action:@selector(addStyle:) forControlEvents:UIControlEventTouchUpInside];
            [cell.deleItem addTarget:self action:@selector(deleItem:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }
    }
}

-(void)textChange{
    int i = 0;
    for(HQStyleObject *sty in _dataSource){
        HQAddStyleCell * cell = [_tableView viewWithTag:1000+i];
        sty.zujianVaule = cell.itemInput.text;
        NSLog(@"%@",sty.zujianVaule);
        i++;
    }
    [_tableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(!_addStyleFlg&&_deleteAllFlg){
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"若要增加组件，需要清除数据，是否清除？ "  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self showHudText:@""];
            [HQNetWork requetParam:[HQParam deleteStyleCmone:_styleID] withMethod:[HQMethod stylecomDelCom] suc:^(NSDictionary *sucData) {
                [self hiddnHud];
                if([sucData[kStatus]integerValue] == 1 || [sucData[kStatus]integerValue] == 0){
                    [self tostView:@"组件删除成功"];
                    
                    //////////////////////////////////////////////////////////////////////////////////////////////
                    // 还原样式列表数据
                    [_dataSource removeAllObjects];
                    HQStyleObject *h = [[HQStyleObject alloc]init];
                    h.itemType = @"edit";
                    [_dataSource addObject:h];
                    // 还原区间差列表数据
                    newsView.dataSource = [[NSMutableArray alloc]init];
                    // 还原设置数据
                    _currentIndex = 0;
                    _addStyleFlg = NO;
                    _deleteAllFlg = NO;
                    _styleID = @"";
                    _newID = @"";
                    _editTag = NO;
                    if(_currentIndex!=_sIntervalArray.count){
                        _sIntervalCode = _sIntervalArray[_currentIndex][@"i_eid"];
                        [_sInterval setTitle:_sIntervalArray[_currentIndex][@"i_num"] forState:UIControlStateNormal];
                    }
                    _sUnit.userInteractionEnabled = YES;
                    [_tableView reloadData];
                    //////////////////////////////////////////////////////////////////////////////////////////////
                    
                }else{
                    [self tostView:sucData[kInfo]];
                }
            } fail:^{
                [self hiddenErrorHud];
            }];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    }else{
        if(indexPath.row == _dataSource.count){
            int i =0;
            for(HQStyleObject *sty in _dataSource){
                HQAddStyleCell * cell = [_tableView viewWithTag:1000+i];
                sty.zujianVaule = cell.itemInput.text;
                i++;
            }
            HQStyleObject *styleObject = [[HQStyleObject alloc]init];
            [_dataSource addObject:styleObject];
            [_tableView reloadData];
        }
    }
}

/**
 选择组件
 
 @param button 按钮
 */
-(void)addStyle:(HQButton *)button{
    _currentCell = button.className;
    [self.view endEditing:YES];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam allComponents:[UserModelManager userID]] withMethod:[HQMethod compontIndex] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSArray *data = sucData[@"data"];
            if(data.count == 0){
                [self tostView:@"暂无组件数据，请新建组件"];
            }else{
                NSMutableArray *m = [[NSMutableArray alloc]init];
                _styleItem0Array = [[NSMutableArray alloc]init];
                for(NSDictionary *dic in data){
                    [_styleItem0Array addObject:dic];
                    [m addObject:[NSString stringWithFormat:@"%@",dic[@"c_name"]]];
                }
                ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
                [_zhPickerView setPickeTitle:@"选择组件"];
                [_zhPickerView selectObj:_currentCell.selctStyleItem.titleLabel.text];
                _zhPickerView.delegate = self;
                _zhPickerView.tag = 1004;
                [_zhPickerView show];
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 选择区间
 
 @param sender 按钮
 */
- (IBAction)sUnitItem:(id)sender {
    [self.view endEditing:YES];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam findInterUserId:[UserModelManager userID]] withMethod:[HQMethod findInter] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSArray *data = sucData[@"data"];
            if(data.count == 0){
                [self tostView:@"暂无区间数据，请联系管理员后台维护数据"];
            }else{
                NSMutableArray *m = [[NSMutableArray alloc]init];
                _sUnitArray = [[NSMutableArray alloc]init];
                for(NSDictionary *dic in data){
                    [_sUnitArray addObject:dic];
                    [m addObject:[NSString stringWithFormat:@"%@",dic[@"i_num_unit"]]];
                }
                ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
                [_zhPickerView setPickeTitle:@"规格"];
                [_zhPickerView selectObj:_sUnit.titleLabel.text];
                _zhPickerView.tag = 1001;
                _zhPickerView.delegate = self;
                [_zhPickerView show];
                
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/******************************************************************************************************/
/*******************************************保存样式数据*************************************************/
/******************************************************************************************************/
/**
 保存当前数据
 
 @param sender 按钮
 */
- (IBAction)saveCurrentData:(id)sender {
    [self.view endEditing:YES];
    if(_styleImage.image == nil){
        [self tostView:@"请上传图片"];
        return;
    }else if([self isInputEmpty:_styleName.text]){
        [self tostView:@"请输入样式名称"];
        return;
    }else if([self isInputEmpty:_styleIntrduce.text]){
        [self tostView:@"请输入样式介绍"];
        return;
    }else if([_sUnit.titleLabel.text isEqualToString:@"请选择"]){
        [self tostView:@"请选择区间"];
        return;
    }else if([_sInterval.titleLabel.text isEqualToString:@"请选择"]){
        [self tostView:@"请选择区间差"];
        return;
    }
    
    // 判断是否存在重复样式核心逻辑
    if(newsView.dataSource.count >0){
        if(![self.pushType isEqualToString:@"EDIT"]){
            if(_editTag==NO){
                for(NSDictionary *dic in newsView.dataSource){
                    if([dic[@"name_ext"] isEqualToString:_sInterval.titleLabel.text]){
                        [self tostView:@"区间已经存在不能重复添加"];
                        return;
                    }
                }
            }
        }
    }
    
    if(_editTag == YES){
        _newID = _sIntervalCode;
    }
    for(int i =0;i<_dataSource.count;i++){
        HQAddStyleCell * cell = [_tableView viewWithTag:1000+i];
        if([self isInputEmpty:cell.itemInput.text] || [cell.itemSelect.titleLabel.text isEqualToString:@"请选择"]){
            [self tostView:@"界面中有未填项,请填写后提交"];
            return;
        }
    }
    for(int i =0;i<_dataSource.count;i++){
        HQAddStyleCell * cell = [_tableView viewWithTag:1000+i];
        HQStyleObject *sty = _dataSource[i];
        sty.zujianVaule = cell.itemInput.text;
    }
    NSMutableString *temp = [[NSMutableString alloc]init];
    NSMutableString *temp2 = [[NSMutableString alloc]init];
    NSMutableString *temp3 = [[NSMutableString alloc]init];
    
    for(int i =0;i<_dataSource.count;i++){
        HQStyleObject *sty = _dataSource[i];
        [temp appendFormat:@"%@,",sty.zujianVaule];
        [temp2 appendFormat:@"%@,",sty.zujianCode];
        if(sty.mID != nil){
            [temp3 appendFormat:@"%@,",sty.mID];
        }
    }
    NSString *cIDArr = @"";
    NSString *cValueArr = @"";
    NSString *tempStr = [NSString stringWithString:temp];
    NSString *tempStr2 = [NSString stringWithString:temp2];
    if(temp2.length>0){
        tempStr = [tempStr substringToIndex:tempStr.length - 1];
        tempStr2 = [tempStr2 substringToIndex:tempStr2.length - 1];
        cValueArr = [NSString stringWithFormat:@"%@",tempStr];
        cIDArr = [NSString stringWithFormat:@"%@",tempStr2];
    }
    NSString *mIDArr = @"";
    NSString *tempStr3 = [NSString stringWithString:temp3];
    if(temp3.length >0){
        tempStr3 = [tempStr3 substringToIndex:tempStr3.length - 1];
        mIDArr = [NSString stringWithFormat:@"%@",tempStr3];
    }
    NSLog(@"cvalue【%@】++++cid【%@】+++++mid【%@】",cValueArr,cIDArr,mIDArr);
    [self showHudText:@"上传图片..."];
    HQNetWork *hqnetwork = [[HQNetWork alloc]init];
    [hqnetwork httpUploadImages:_styleImage.image URL:[HQMethod uploadface] param:nil suc:^(NSDictionary *suc) {
        [self hiddnHud];
        if([suc[kStatus]integerValue] == 1){
            NSString *imageUrl = [[suc objectForKey:@"data"] objectForKey:@"up_imgurl"];
            NSDictionary *param =[HQParam stylecomAddComWithID:[UserModelManager userID]
                                                       withSId:_styleID
                                                     withSName:_styleName.text
                                                withSIntroduce:_styleIntrduce.text
                                                       withIId:_sUnitCode
                                                      withIeId:_sIntervalCode
                                                  withCIDArray:cIDArr
                                                 withEMaterIal:cValueArr
                                                     withNewID:_newID
                                                      withSImg:imageUrl
                                                       withMID:mIDArr];
            NSLog(@"======= %@",param);
            [self showHudText:@"加载中..."];
            [HQNetWork requetParam:param withMethod:[HQMethod addStyleCom] suc:^(NSDictionary *sucData){
                [self hiddnHud];
                if([sucData[kStatus]integerValue] == 1){
                    _styleID = sucData[@"data"][@"sid"];
                    if([self.pushType isEqualToString:@"EDIT"]){
                        [self tostView:@"编辑样式成功"];
                        [_tableView reloadData];
                    }else{
                        // 添加成功后组件+1
                        if(_editTag==NO){
                            _currentIndex ++;
                            [self tostView:@"添加样式成功"];
                        }else{
                            [self tostView:@"编辑样式成功"];
                        }
                        
                        if(_currentIndex!=_sIntervalArray.count){
                            _sIntervalCode = _sIntervalArray[_currentIndex][@"i_eid"];
                            [_sInterval setTitle:_sIntervalArray[_currentIndex][@"i_num"] forState:UIControlStateNormal];
                        }
                        
                        _addStyleFlg = YES;
                        for(HQStyleObject *o in _dataSource){
                            o.itemType = @"edit";
                            o.zujianVaule = @"";
                        }
                        [_tableView reloadData];
                    }
                    [self test];
                }else{
                    [self tostView:sucData[kInfo]];
                }
            } fail:^{
                [self hiddenErrorHud];
            }];
        }else{
            [self tostView:suc[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == _dataSource.count){
        return 100;
    }else{
        return 51;
    }
}

/******************************************************************************************************/
/*******************************************picker弹窗赋值**********************************************/
/******************************************************************************************************/
/**
 点击picker时候添加设置数据
 
 @param pickView picker
 @param resultString 结果值
 */
-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    if(pickView.tag == 1001){
        for(NSDictionary *dic in _sUnitArray){
            if([dic[@"i_num_unit"] isEqualToString:resultString]){
                _sUnitCode = dic[@"i_id"];
                break;
            }
        }
        [_sUnit setTitle:resultString forState:UIControlStateNormal];
        // 设置区间差值（自动选择）
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam finInext:_sUnitCode] withMethod:[HQMethod findInext] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                _sIntervalArray = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
                // 设置区间差的值
                if(_sIntervalArray.count>0){
                    _sIntervalCode = _sIntervalArray[_currentIndex][@"i_eid"];
                    [_sInterval setTitle:_sIntervalArray[_currentIndex][@"i_num"] forState:UIControlStateNormal];
                }
            }else{
                [_sInterval setTitle:@"请选择" forState:UIControlStateNormal];
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [_sInterval setTitle:@"请选择" forState:UIControlStateNormal];
            [self hiddenErrorHud];
        }];
    }else if(pickView.tag == 1004){
        // 列表样式
        NSString *tempID = @"";
        NSString *tempUnit = @"";
        for(NSDictionary *dic in _styleItem0Array){
            NSString *pattonStr = [NSString stringWithFormat:@"%@",dic[@"c_name"]];
            if([pattonStr isEqualToString:resultString]){
                tempID = dic[@"c_id"];
                tempUnit = dic[@"c_unit"];
                break;
            }
        }
        HQStyleObject *s = _currentCell.dataInfo;
        s.zujianName = resultString;
        s.zujianCode = tempID;
        s.unit = tempUnit;
        _currentCell.unit.text = tempUnit;
        [_currentCell.selctStyleItem setTitle:resultString forState:UIControlStateNormal];
    }
}

/******************************************************************************************************/
/*******************************************获取样式列表数据**********************************************/
/******************************************************************************************************/
/**
 获取已经创建的列表数据
 */
-(void)test{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam stylecomFindStyle:_styleID withUid:[UserModelManager userID]] withMethod:[HQMethod findStyleCom] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[@"data"][@"code"] integerValue] == 1){
            NSLog(@"%@",sucData[@"data"][@"data"][@"qjc"]);
            newsView.dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"][@"data"][@"qjc"]];
            [self calculateHeight];
            if(newsView.dataSource.count > 0){
                _sUnit.userInteractionEnabled = NO;
                NSDictionary *dict = sucData[@"data"][@"data"];
                _styleName.text = dict[@"s_name"];
                _styleIntrduce.text = dict[@"s_introduce"];
                _listBgView.hidden = NO;
                _editTag = NO;
                if([self.pushType isEqualToString:@"EDIT"]){
                    [newsView selectIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
                    // 编辑时默认选中第一条
                    [newsView.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionTop];
                }
            }else{
                _sUnit.userInteractionEnabled = YES;
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/******************************************************************************************************/
/*******************************************选择图片部分*************************************************/
/******************************************************************************************************/
/**
 选择图片或拍照
 
 @param sender 按钮
 */
- (IBAction)selectImage:(id)sender {
    NSArray *objs = [[NSBundle mainBundle]loadNibNamed:@"HQAddPhotoView" owner:nil options:nil];
    HQAddPhotoView *photoView = objs[0];
    photoView.delegate = self;
    photoView.frame = [UIScreen mainScreen].bounds;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:photoView];
}

/**
 拍照上传图片
 */
-(void)paizhao{
    NSLog(@"拍照");
    [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeCamera context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
        if (isSuccess) {
            /** 上传图片*/
            _styleImage.image = image;
        } else {
            [self tostView:@"调取本地相册失败"];
        }
    }];
}

/**
 相册选择图片
 */
-(void)xiangce{
    NSLog(@"照片");
    [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeLocal context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
        if (isSuccess) {
            /** 上传图片*/
            _styleImage.image = image;
        } else {
            [self tostView:@"调取本地相册失败"];
        }
    }];
}
/******************************************************************************************************/
/******************************************************************************************************/
/**
 删除某一行未保存的样式
 
 @param button 按钮
 */
-(void)deleItem:(HQButton *)button{
    [_dataSource removeObject:button.dataInfo];
    [_tableView reloadData];
}

-(void)calculateHeight{
    NSInteger count = newsView.dataSource.count;
    NSInteger line = count/3 + (count % 3 != 0 ? 1 : 0);
    _listBgViewConstraint.constant = (line*55)-5;
    CGRect frame = _currentRect;
    frame.size.height = (_currentRect.size.height + (line*55))-5-60;
    newsView.frame = CGRectMake(0, 0, _listBgView.bounds.size.width, _listBgView.bounds.size.height);
    _headerView.frame = frame;
    [_tableView reloadData];
}

@end
